using System;
using System.Activities;
using System.Activities.Presentation;
using System.Activities.Presentation.Model;
using System.Activities.Presentation.Services;
using System.Activities.Statements;

namespace Headsight.BPMN2WF.Testing
{
    public class Flowchart4TestHelper
    {
        private readonly WorkflowDesigner _wfDesginer;
        private readonly ModelService _mSrv;
        private ModelItem _lastNode;

        public Flowchart4TestHelper()
        {
            _wfDesginer = new WorkflowDesigner();

            var actBuild = new ActivityBuilder
            {
                Name =
                    String.Format("Headsight.BPMN2WF.TestObjCtors.{0}", typeof (Flowchart4TestHelper).Name)
            };

            _wfDesginer.Load(actBuild);

            _mSrv = _wfDesginer.Context.Services.GetService<ModelService>();


            var root = _mSrv.Root;
            root.Properties["Implementation"].SetValue(new Flowchart());
        }

        public ModelItem Root { get { return _mSrv.Root; } }

        public ModelItem Implementation { get { return _mSrv.Root.Properties["Implementation"].Value; } }

        public ModelItem CreateStartNode()
        {
            var mItem_flowchart = _mSrv.Root.Properties["Implementation"].Value;

            mItem_flowchart.Properties["StartNode"].SetValue(new FlowStep());

            var result = _lastNode = mItem_flowchart.Properties["StartNode"].Value;

            return result;
        }

        public ModelItem CreateNextNode()
        {
            _lastNode.Properties["Next"].SetValue(new FlowStep());

            return _lastNode = _lastNode.Properties["Next"].Value;
        }

        public ModelItem[] CreateTrueAndFalse()
        {
            var result = new ModelItem[3];

            _lastNode.Properties["Next"].SetValue(new FlowDecision(true));
            result[0] = _lastNode = _lastNode.Properties["Next"].Value;

            _lastNode.Properties["True"].SetValue(new FlowStep());
            _lastNode.Properties["False"].SetValue(new FlowStep());

            result[1] = _lastNode.Properties["True"].Value;
            result[2] = _lastNode.Properties["False"].Value;

            _lastNode = result[2];

            return result;
        }

        public ModelItem[] CreateExclusiveGateway()
        {
            var result = new ModelItem[3];

            _lastNode.Properties["Next"].SetValue(new FlowSwitch<String>());
            result[0] = _lastNode = _lastNode.Properties["Next"].Value;

            _lastNode.Properties["Default"].SetValue(new FlowStep());
            _lastNode.Properties["Cases"].SetValue(new FlowStep());

            result[1] = _lastNode.Properties["Default"].Value;
            result[2] = _lastNode.Properties["Cases"].Value;

            _lastNode = result[2];

            return result;
        }
    }
}